﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Passwords.Models;
using Silvermoon.Windows;
using System.Drawing;
using Silvermoon;
using Silvermoon.UI;
using Silvermoon.Animations;
using Silvermoon.OpenGL;
using Passwords.Utils;
using Silvermoon.Controls.Forms;
using Silvermoon.Windows.Styles;
using Passwords.Controllers;
using Passwords.Biz;
using Silvermoon.Shapes;

namespace Passwords.Pages
{
    public class StringPickerPage : ItemPickerPage
    {
        #region fields

        private RotateTransform rotation;
        private ScrollPanel panel;
        private Label label;
        private TextBox textBox;

        #endregion
        #region ctor

        public StringPickerPage()
            : base()
        {
            InitializePage();
        }

        public StringPickerPage(bool isNumeric)
            : base()
        {
            this.IsNumeric = isNumeric;
            InitializePage();
        }


        #endregion
        #region statics

        public static PropertyAccessor<StringPickerPage, float> RotationProperty = PropertyAccessor.Register<StringPickerPage, float>("Rotation",
            (c) => c.Rotation,
            (c, v) => c.Rotation = v);


        #endregion
        #region Props

        public bool IsNumeric { get; private set; }

        public float Rotation
        {
            get { return rotation.Angle; }
            set
            {
                if (rotation.Angle != value)
                {
                    rotation.Angle = value;
                    OnRotationChanged();
                }
            }
        }

        public override bool CanFocus
        {
            get
            {
                return false;
            }
        }


        #endregion
        #region methods

        private void InitializePage()
        {
            Title = "PASSWORDS < DETAILS < EDIT";
            Color = PhoneColors.Transparent;

            rotation = new RotateTransform { Axis = RotateAxis.XAxis, Angle = 90 };
            //int h = FontSprite.Regular.CharHeight;
            int h = 0;
            panel = new ScrollPanel
            {
                Margin = new Margin(0, h, 0, null),
                Height = 400,
                Color = PhoneColors.AlphaColor(PhoneColors.Dark, 250),
                Transformation = rotation,
                IsFocusable = false,
                Style = StylePool.EditWindow
            };
            panel.TouchDown += (s, e) => e.FocusSet = true;
            Controls.Add(panel);

            label = new Label { Text = "Name:", AutoSize = true, TextColor = PhoneColors.Accent, Location = new Point(10, 10) };
            textBox = new TextBox { Margin = new Margin(10, 64, 10, null), Height = 64 * 3, AutoHeight = true, Font = FontCache.MediumRegularFont, AcceptReturn = true };
            textBox.Watermark = new StringShape { Text = "Enter Text", Opacity = 160, Color = PhoneColors.Dark };
            textBox.KeyPress += new EventHandler<System.Windows.Forms.KeyPressEventArgs>(textBox_KeyPress);
            panel.Controls.Add(label, textBox);
            if (IsNumeric) textBox.AllowedCharacters = "0123456789";
        }

        void textBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == '\n')
            {
                e.Handled = true;
                OnOK(this, null);
            }
        }

        protected override void OnOK(object sender, CommandEventArgs e)
        {
            if (PasswordItem != null)
            {
                PasswordItem.Value = textBox.Text;
            }
            base.OnOK(sender, e);
        }

        protected override void OnShown()
        {
            base.OnShown();
            textBox.PreLoad();
            textBox.Focus();
            textBox.SelectAll();
            textBox.TextChanged += new EventHandler(textBox_TextChanged);
            IsKeyboardVisible = true;
        }

        void textBox_TextChanged(object sender, EventArgs e)
        {
            IsSaveEnabled = true;
        }

        protected override void OnPasswordItemChanged(PasswordItem item)
        {
            if (item != null)
            {
                label.Text = item.Name;
                textBox.Text = item.Value;
            }
            else
            {
                label.Text = "";
                textBox.Text = "";
            }
            base.OnPasswordItemChanged(item);
        }

        private void OnRotationChanged()
        {
            panel.Invalidate(InvalidationFlag.Transformation);
        }


        #endregion
    }
}
